import { ApiCall } from "tsrpc";
import { ReqCatchPet, ResCatchPet } from "../../../shared/master/word/PtlCatchPet";
import { player } from "../../../model/fight/body/player";
import { template } from "../../../shared/master/MsgAction";
import { enemy } from "../../../model/fight/body/enemy";
import app, { xlsxCfgNames } from "../../../app";
import { pet } from "../../../model/fight/body/pet";
import manage_task from "../../model/manage/manage_task";
import { rank_type, taskKey } from "../../../shared/interface";
import god from "../../../god";
let catchMap = new Map();
export default async function (call: ApiCall<ReqCatchPet, ResCatchPet>) {
    call.succ({});
    const user = call.req._player as player;
    const _cell = user.getPosition();
    const _wrod = _cell.getWord()
    const cellBodys = _cell.getBodys();
    const idx = call.req.idx;
    const catchPet = cellBodys[idx - 1];
    const p = user.name;
    const p_uuid = user.uuid;
    if (!catchPet) {
        user.sendMsg(template.default, `当前位置没有ID为${idx}的怪物`);
        return;
    }
    if (catchMap.has(p_uuid)) {
        user.sendMsg(template.default, `正在捕捉中...`);
        return;
    }
    const e = catchPet.name;
    const e_uuid = catchPet.uuid;
    let log = user.check_wallet(0, 0, [{ id: 'prop-5', v: 1 }], false, '宠物捕捉', true);
    if (!log.state) {
        catchMap.delete(p_uuid)
        return;
    }
    let check = async () => {
        catchMap.delete(p_uuid)
        if (!catchPet) {
            await user.sendMsg(template.default, `捕捉失败:${e} 已消失.`);
            return true;
        }
        let __cell = user.getPosition();
        if (__cell.y != _cell.y || __cell.x != _cell.x) {
            await user.sendMsg(template.default, `捕捉失败:${e} 你已不在当前位置.`);
            return true;
        }
        if (user.get_battle()) {
            await user.sendMsg(template.default, `捕捉失败:${e} 你已开始战斗.`);
            return true;
        }
        if (catchPet.is_die()) {
            await user.sendMsg(template.default, `捕捉失败:${e} 已死亡.`);
            return true;
        }
        if (catchPet.get_battle()) {
            await user.sendMsg(template.default, `捕捉失败:${e} 已经开始战斗.`);
            return true;
        }
        if ((catchPet instanceof enemy) || (catchPet instanceof pet)) {

        } else {
            await user.sendMsg(template.default, `捕捉失败:${e} 为不可捕捉的单位.`);
            return true;
        }
        if (app.random(0, 100) <= 10) {
            await user.sendMsg(template.default, `捕捉失败:${e} 逃跑了.`);
            return true;
        }
        catchMap.set(p_uuid, 1)
        return false
    }




    const table = [...(app.xlsxCfgMap.get(xlsxCfgNames.捕捉宠物文案) as Map<string, any>).values()];
    // 捕捉准备
    let cfg;
    let replacedStr;
    const time = god.getServerCfgItem('CatchPet_time').b

    for (let i = 0; i < 5; i++) {
        // 扔出精灵球
        if (app.isDebug(p_uuid)) {
            continue;
        }
        cfg = table[app.random(0, table.length - 1)];
        replacedStr = cfg[`n${i + 1}`].replace(/玩家/g, `🧙${p}`).replace(/怪物/g, `👻${e}`);
        await user.sendMsg(template.default, replacedStr);
        if (await check()) {
            return;
        }
        await new Promise(rs => { setTimeout(rs, time) });
    }


    if ((catchPet instanceof enemy) || (catchPet instanceof pet)) {

    } else {
        user.sendMsg(template.default, `捕捉失败:${e} 为不可捕捉的单位.`);
        return true;
    }
    user.log.add(rank_type.捕捉宠物成功次数,1)
    catchMap.delete(p_uuid)
    catchPet.createCfg.leve = catchPet.leve.num;
    const item = app.createCfgItem('pet', catchPet.getScore(), _wrod.name, catchPet.createCfg);
    user.getBag().add(item);
    user.sendMsg(template.default, `捕捉成功!你获得${item.getStyleName()}`);
    manage_task.event(taskKey.成功捕捉宠物, 1, user, catchPet)
    _cell.del(catchPet)
    catchPet.destroy();
}